package com.monitor.system.service.impl;

import com.monitor.common.utils.DateUtils;
import com.monitor.common.utils.StringUtils;
import com.monitor.system.domain.ApplicationAbnormalManagement;
import com.monitor.system.domain.ThresholdManagement;
import com.monitor.system.domain.vo.ApplicationAbnormalManagementVo;
import com.monitor.system.mapper.ApplicationAbnormalManagementMapper;
import com.monitor.system.service.IApplicationAbnormalManagementService;
import com.monitor.system.service.IThresholdManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import static com.monitor.common.constant.Constants.FOUR;
import static com.monitor.common.constant.Constants.ONE;
import static com.monitor.common.constant.Constants.THREE;
import static com.monitor.common.constant.Constants.TWO;
import static com.monitor.common.constant.Constants.ZERO;

/**
 * 应用异常管理Service业务层处理
 * 
 * @author monitor
 * @date 2024-06-11
 */
@Service
public class ApplicationAbnormalManagementServiceImpl implements IApplicationAbnormalManagementService 
{
    @Autowired
    private ApplicationAbnormalManagementMapper applicationAbnormalManagementMapper;

    @Autowired
    private IThresholdManagementService service;

    /**
     * 查询应用异常管理
     * 
     * @param id 应用异常管理主键
     * @return 应用异常管理
     */
    @Override
    public ApplicationAbnormalManagement selectApplicationAbnormalManagementById(Long id)
    {
        return applicationAbnormalManagementMapper.selectApplicationAbnormalManagementById(id);
    }

    /**
     * 查询应用异常管理列表
     * 
     * @param applicationAbnormalManagement 应用异常管理
     * @return 应用异常管理
     */
    @Override
    public List<ApplicationAbnormalManagement> selectApplicationAbnormalManagementList(ApplicationAbnormalManagement applicationAbnormalManagement)
    {
        List<ApplicationAbnormalManagement> appNewList = new ArrayList<>();
        List<ApplicationAbnormalManagement> appList = applicationAbnormalManagementMapper.selectApplicationAbnormalManagementList(applicationAbnormalManagement);
        if ( appList != null && appList.size() > 0) {
            for (ApplicationAbnormalManagement appManagement : appList) {
                ThresholdManagement thresholdManagement = new ThresholdManagement();
                thresholdManagement.setType("threshold_management_application");
                thresholdManagement.setHost(appManagement.getId().toString());
                ThresholdManagement threshold = service.selectThresholdManagement(thresholdManagement);

                if(StringUtils.isNotNull(threshold)) {
                    // 启动时间
                    BigDecimal startTime = new BigDecimal(appManagement.getExtend4());
                    // 响应时间
                    BigDecimal responseTime = new BigDecimal(appManagement.getExtend5());
                    // 启动时间 > 设定阈值
                    // 状态：故障
                    if((startTime.compareTo(new BigDecimal(threshold.getTimeConsuming1())) >= 0) ) {
                        appManagement.setHealthStatus(FOUR);
                    }
                    // 状态：异常
                    else if((startTime.compareTo(new BigDecimal(threshold.getTimeConsuming3())) >= 0) &&
                            (startTime.compareTo(new BigDecimal(threshold.getTimeConsuming2())) <= 0)) {
                            appManagement.setHealthStatus(THREE);
                    }
                    // 状态：告警
                    else if((startTime.compareTo(new BigDecimal(threshold.getTimeConsuming5())) >= 0) &&
                            (startTime.compareTo(new BigDecimal(threshold.getTimeConsuming4())) <= 0)) {
                            appManagement.setHealthStatus(TWO);
                    }
                    // 状态：延时
                    else if((startTime.compareTo(new BigDecimal(threshold.getExtend1())) >= 0) &&
                            (startTime.compareTo(new BigDecimal(threshold.getTimeConsuming6())) <= 0) ) {
                            appManagement.setHealthStatus(ONE);
                    }
                    // 状态：低于最小值状态设为正常
                    else if((startTime.compareTo(new BigDecimal(threshold.getExtend1())) < 0)) {
                        appManagement.setHealthStatus("0");
                    }
                    // 响应时间 > 设定阈值
                    // 状态：故障
                    if((responseTime.compareTo(new BigDecimal(threshold.getExtend2())) >= 0) ) {
                        appManagement.setHealthStatus(FOUR);
                    }
                    // 状态：异常
                    else if((responseTime.compareTo(new BigDecimal(threshold.getExtend4())) >= 0) &&
                            (responseTime.compareTo(new BigDecimal(threshold.getExtend3())) <= 0)) {
                        if(appManagement.getHealthStatus().compareTo(THREE) > 0) {
                            appManagement.setHealthStatus(FOUR);
                        } else {
                            appManagement.setHealthStatus(THREE);
                        }
                    }
                    // 状态：告警
                    else if((responseTime.compareTo(new BigDecimal(threshold.getExtend6())) >= 0) &&
                            (responseTime.compareTo(new BigDecimal(threshold.getExtend5())) <= 0)) {
                        if(appManagement.getHealthStatus().compareTo(TWO) > 0) {
                            appManagement.setHealthStatus(THREE);
                        } else {
                            appManagement.setHealthStatus(TWO);
                        }
                    }
                    // 状态：延时
                    else if((responseTime.compareTo(new BigDecimal(threshold.getExtend8())) >= 0) &&
                            (responseTime.compareTo(new BigDecimal(threshold.getExtend7())) <= 0)) {
                        if(appManagement.getHealthStatus().compareTo(ONE) > 0) {
                            appManagement.setHealthStatus(TWO);
                        } else {
                            appManagement.setHealthStatus(ONE);
                        }
                    }

                    // 状态：低于最小值 状态设为正常
                    else if((responseTime.compareTo(new BigDecimal(threshold.getExtend7())) < 0)) {
                        if(appManagement.getHealthStatus().compareTo(ZERO) < 0) {
                            appManagement.setHealthStatus(ZERO);
                        }
                    }
                }
                applicationAbnormalManagementMapper.updateApplicationAbnormalManagement(appManagement);
                appNewList = applicationAbnormalManagementMapper.selectAbnormalList(applicationAbnormalManagement);
            }
        }
        return appNewList;
    }

    /**
     * 新增应用异常管理
     * 
     * @param applicationAbnormalManagement 应用异常管理
     * @return 结果
     */
    @Override
    public int insertApplicationAbnormalManagement(ApplicationAbnormalManagement applicationAbnormalManagement)
    {
        applicationAbnormalManagement.setCreateTime(DateUtils.getNowDate());
        return applicationAbnormalManagementMapper.insertApplicationAbnormalManagement(applicationAbnormalManagement);
    }

    /**
     * 修改应用异常管理
     * 
     * @param applicationAbnormalManagement 应用异常管理
     * @return 结果
     */
    @Override
    public int updateApplicationAbnormalManagement(ApplicationAbnormalManagement applicationAbnormalManagement)
    {
        applicationAbnormalManagement.setUpdateTime(DateUtils.getNowDate());
        return applicationAbnormalManagementMapper.updateApplicationAbnormalManagement(applicationAbnormalManagement);
    }

    /**
     * 批量删除应用异常管理
     * 
     * @param ids 需要删除的应用异常管理主键
     * @return 结果
     */
    @Override
    public int deleteApplicationAbnormalManagementByIds(Long[] ids)
    {
        return applicationAbnormalManagementMapper.deleteApplicationAbnormalManagementByIds(ids);
    }

    /**
     * 删除应用异常管理信息
     * 
     * @param id 应用异常管理主键
     * @return 结果
     */
    @Override
    public int deleteApplicationAbnormalManagementById(Long id)
    {
        return applicationAbnormalManagementMapper.deleteApplicationAbnormalManagementById(id);
    }

    /**
     *
     *
     * @param applicationAbnormalManagement 管理
     * @return 结果
     */
    @Override
    public int selectFault(ApplicationAbnormalManagementVo applicationAbnormalManagement)
    {
        return applicationAbnormalManagementMapper.selectFault(applicationAbnormalManagement);
    }

    /**
     *
     *
     * @param applicationAbnormalManagement 管理
     * @return 结果
     */
    @Override
    public int selectAbnormal(ApplicationAbnormalManagementVo applicationAbnormalManagement)
    {
        return applicationAbnormalManagementMapper.selectAbnormal(applicationAbnormalManagement);
    }

    /**
     *
     *
     * @param applicationAbnormalManagement 管理
     * @return 结果
     */
    @Override
    public int selectAlarm(ApplicationAbnormalManagementVo applicationAbnormalManagement)
    {
        return applicationAbnormalManagementMapper.selectAlarm(applicationAbnormalManagement);
    }

    /**
     *
     *
     * @param applicationAbnormalManagement 管理
     * @return 结果
     */
    @Override
    public int selectDelay(ApplicationAbnormalManagementVo applicationAbnormalManagement)
    {
        return applicationAbnormalManagementMapper.selectDelay(applicationAbnormalManagement);
    }


    @Override
    public List<ApplicationAbnormalManagement> selectApplicationAbnormalManagementALLList(ApplicationAbnormalManagement applicationAbnormalManagement) {
        return applicationAbnormalManagementMapper.selectApplicationAbnormalManagementList(applicationAbnormalManagement);
    }

}
